﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using FluentValidation;

namespace PdxDemo.FluentValidationDemo
{
    public class ViewModelValidator : AbstractValidator<ViewModel>
    {
        public ViewModelValidator()
        {
            RuleFor(x => x.Intval)
                .NotEmpty()
                .NotEqual(123).WithMessage("123 is not allowed, for some reason");

            RuleFor(x => x.Stringval)
                .Length(1, 12).WithMessage("Stringval must be between 1 and 12 characters");

            RuleFor(x => x.MyBoolean)
                .NotEqual(true).When(vm => vm.Stringval.Length != 12)
                .WithMessage("If MyBoolean is true, Stringval must be 12 chars");
        }
    }

    public interface IHaveValidator
    {
        IValidator GetValidator();
    }

    public enum TEST_ENUM
    {
        [Description("First Choice")]
        First,

        [Description("Second Choice")]
        Second,

        [Description("Third Choice")]
        Third,
    }

    public class ViewModel : PDX.INPC, IHaveValidator
    {
        static ViewModelValidator validator = new ViewModelValidator();

        public IValidator GetValidator() { return validator; }

        public ViewModel()
        {
            Intval = 123;
            Stringval = "a string";

            MyEnum = TEST_ENUM.Second;
        }

        int intval;
        public int Intval
        {
            get
            {
                return intval;
            }
            set
            {
                if (intval != value)
                {
                    intval = value;
                    OnPropertyChanged("Intval");
                }
            }
        }

        string stringval;
        [DisplayName("A String Value")]
        public string Stringval
        {
            get
            {
                return stringval;
            }
            set
            {
                if (stringval != value)
                {
                    stringval = value;
                    OnPropertyChanged("Stringval");
                }
            }
        }

        bool myBoolean;
        [DisplayName("My Boolean")]
        public bool MyBoolean
        {
            get
            {
                return myBoolean;
            }
            set
            {
                if (myBoolean != value)
                {
                    myBoolean = value;
                    OnPropertyChanged("MyBoolean");
                }
            }
        }

        TEST_ENUM myEnum;
        [DisplayName("Choose Wisely...")]
        public TEST_ENUM MyEnum
        {
            get
            {
                return myEnum;
            }
            set
            {
                if (myEnum != value)
                {
                    myEnum = value;
                    OnPropertyChanged("MyEnum");
                }
            }
        }
    }
}
